package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.entity.MallBanner;
import com.sc.nft.entity.dto.MallBannerAddDTO;
import com.sc.nft.entity.dto.MallBannerUpdateDTO;
import com.sc.nft.entity.dto.MallBannerUpdateStatusDTO;
import com.sc.nft.entity.vo.MallBannerVO;
import com.sc.nft.service.MallBannerService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/mallBanner")
@Api(tags = "商城banner")
public class MallBannerController {

    private final MallBannerService mallBannerService;

    @ApiOperation(value = "根据id获取数据", notes = "根据id获取数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MallBannerVO> getById(@Valid @RequestBody IdRequest idRequest) {
        MallBanner mallBanner = mallBannerService.getByIdAndStatus(idRequest.getId(), 1);
        MallBannerVO mallBannerVO = new MallBannerVO();
        BeanUtil.copyProperties(mallBanner, mallBannerVO);
        return Result.success(mallBannerVO);
    }

    @ApiOperation(value = "获取分页列表", notes = "获取分页列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<Page<MallBannerVO>> page(@RequestBody PageRequest pageRequest) {
        return PageResult.success(mallBannerService.page(pageRequest));
    }

    @ApiOperation(value = "根据id删除数据", notes = "根据id删除数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/deleteById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result deleteById(@Valid @RequestBody IdRequest idRequest) {
        mallBannerService.deleteById(idRequest.getId());
        return Result.success();
    }


    @ApiOperation(value = "根据id更新数据", notes = "根据id更新数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result updateById(@Valid @RequestBody MallBannerUpdateDTO mallBannerUpdateDTO) {
        MallBanner mallBanner = new MallBanner();
        BeanUtil.copyProperties(mallBannerUpdateDTO,mallBanner);
        mallBannerService.updateById(mallBanner);
        return Result.success();
    }

    @ApiOperation(value = "根据id上下架", notes = "根据id上下架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateStatusById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result updateStatusById(@Valid @RequestBody MallBannerUpdateStatusDTO mallBannerUpdateDTO) {
        MallBanner mallBanner = new MallBanner();
        BeanUtil.copyProperties(mallBannerUpdateDTO,mallBanner);
        mallBannerService.updateById(mallBanner);
        return Result.success();
    }

    @ApiOperation(value = "新增数据", notes = "新增数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result add(@Valid @RequestBody MallBannerAddDTO mallBannerAddDTO) {
        MallBanner mallBanner = new MallBanner();
        BeanUtil.copyProperties(mallBannerAddDTO,mallBanner);
        mallBannerService.add(mallBanner);
        return Result.success();
    }
}
